package com.moxingzhegroup.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import androidx.annotation.Nullable;

import com.moxingzhegroup.MyApplication;
import com.moxingzhegroup.MyPreferences;
import com.moxingzhegroup.R;
import com.moxingzhegroup.helper.PushHelper;
import com.umeng.message.PushAgent;
import com.umeng.message.api.UPushRegisterCallback;

/**
 * @author LiaoRun
 */
public class BaseActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 注：该方法是【友盟+】Push后台进行日活统计及多维度推送的必调用方法，请务必调用！

        if (hasAgreedAgreement()) {
            PushAgent.getInstance(this).onAppStart();
        } else {
            showAgreementDialog();
        }
    }

    private boolean hasAgreedAgreement() {
        return MyPreferences.getInstance(this).hasAgreePrivacyAgreement();
    }

    private void showAgreementDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.agreement_title);
        builder.setMessage(R.string.agreement_msg);
        builder.setPositiveButton(R.string.agreement_ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                //用户点击隐私协议同意按钮后，初始化PushSDK
                MyPreferences.getInstance(getApplicationContext()).setAgreePrivacyAgreement(true);
                PushHelper.init(getApplicationContext());
                PushAgent.getInstance(getApplicationContext()).register(new UPushRegisterCallback() {
                    @Override
                    public void onSuccess(final String deviceToken) {

                    }

                    @Override
                    public void onFailure(String code, String msg) {
                        Log.d("MainActivity", "code:" + code + " msg:" + msg);
                    }
                });
            }
        });
        builder.setNegativeButton(R.string.agreement_cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        });
        builder.create().show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            finish();
        }
        return false;
    }
}
